/**
 * 
 * @author Sunix
 * @date   2013-5-17
 * @Email  SunixSky@gmail.com
 * 
 * 文件工具
 * 
 */	
package utils{
	import flash.display.BitmapData;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.utils.ByteArray;
	
	public class FileUtil {
		
		/**
		 * 通过后缀名称过滤文件
		 * 
		 * @param originalFiles
		 * @param extendName
		 * @return 
		 * 
		 */		
		public static function filterFileByExtendName(originalFiles:Array,extendName:String):Array{
			var processFiles:Array = new Array();  //筛选后的文件数组
			var file:File;  
			for each(file in originalFiles){
				if(file.isDirectory || file.isHidden)
					continue;
				if(file.extension == extendName){
					processFiles.push(file);
				}
			}
			return processFiles;
		}
		
		//TODO 改用二分
		public static function sortFileByNumber(files:Array, low:int, high:int):Array{
			
			var i:int = low;
			var j:int = high;
			
			if(i>=j)return null;
			
			var f:File = files[i];
			var key:int = int(f.name.substr(0,f.name.indexOf(".")));
			
			while(i<j){
				while(i<j && int(files[j].name.substr(0,files[j].name.indexOf(".")))>key){
					j--;
				}
				if(i<j){
					f=files[i];
					files[i]=files[j];
					files[j]=f;
				}
				while(i<j && int(files[i].name.substr(0,files[i].name.indexOf(".")))<key){
					i++;
				}
				if(i<j){
					f=files[i];
					files[i]=files[j];
					files[j]=f;
				}
				sortFileByNumber(files, low, i-1);
				sortFileByNumber(files, j+1, high);
			}
			return files;
		}
		
		public static function writePNG(bitmapData:BitmapData, path:String):void{
			var bytes:ByteArray=PNGUtil.encode(bitmapData);
			writeBytes(bytes, path);
		}
		
		public static function read(path:String):String{
			var str:String="";
			var file:File = new File(path);
			var fileStream:FileStream = new FileStream();
			try{
				fileStream.open(file, FileMode.READ);
				str=fileStream.readUTFBytes(fileStream.bytesAvailable);
				fileStream.close();
			}catch(e:Error){
				trace(e.message);
			}
			return str;
		}
		
		public static function readXml(path:String):XML{
			var str:String = read(path);
			if(str == "")return null;
			
			return XML(str);
		}
		
		public static function writeBytes(bytes:ByteArray, path:String):Boolean{
			var fl:File = new File(path);
			var fs:FileStream = new FileStream();
			try{  
				fs.open(fl,FileMode.WRITE);  
				fs.writeBytes(bytes);  
				fs.close();  
			} catch (e:Error){  
				trace(e.message);
				return false;
			}
			return true;
		}
		
		public static function writeXml(str:String, path:String):void{
			write('<?xml version="1.0" encoding="utf-8"?>\n'+str, path);
		}
		
		public static function createDir(path:String):void{
			var file:File=new File(path);
			file.createDirectory();
		}
		
		public static function write(str:String, path:String):Boolean{
			var fl:File=new File(path);
			var fs:FileStream=new FileStream();
			try{
				fs.open(fl, FileMode.WRITE);
				fs.writeUTFBytes(str);
				fs.close();
			}catch(e:Error){
				trace(e.message);
				return false;
			}
			return true;
		}
	}
}